package demo06

/**
 * @author xiaoZhao
 * @date 2022/7/19
 * @describe 单词统计
 *            经典版本的wordCount
 */
object Test17_CommonWordCount {
  def main(args: Array[String]): Unit = {
    // 创建单词集合
    val strings = List(
      "hello",
      "hello world",
      "hello scala",
      "hello scala spark",
      "hello scala spark flink"
    )

    // 对字符串进行拆分
    val wordList = strings.flatMap(_.split(" "))

    // 相同单词分组
    val groupMap = wordList.groupBy(word => word)

    // 对分组后List取长度，得到单词的个数
    val countMap = groupMap.map(kv => (kv._1, kv._2.length))

    // 将map转换为List , 通过count进行排序 取前三
    val sortList = countMap.toList
      .sortWith(_._2 > _._2)
      .take(3)

    println(sortList)
  }
}
